//
//  SubMenu.m
//  ShoppingCompare
//
//  Created by Wenjie on 11-6-12.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SubMenu.h"
#import "RootViewController.h"
#import "LKGoogleTranslator.h"
#import "Taobao.h"
#import "Register.h"
#import "Result.h"
#import "CompareList.h"

@implementation SubMenu

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	// Navigation set --------------------------------------
	self.title = @"Sub Menu";
	self.view.backgroundColor = [UIColor whiteColor];
	
	UITableView* tabLanguage = [[[UITableView alloc] initWithFrame:CGRectMake(15.0f, 50.0f, 290.0f, 200.0f) style:UITableViewStyleGrouped] autorelease];
	tabLanguage.backgroundColor = [UIColor clearColor];
	[self.view addSubview:tabLanguage];
	
	tabLanguage.delegate = self;
	tabLanguage.dataSource = self;
/*	// Menu set --------------------------------------
	UIButton* btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	btn1.frame = CGRectMake(50.0f, 50.0f, 220.0f, 40.0f);
	[btn1 addTarget:self action:@selector(pressBtn1) forControlEvents:UIControlEventTouchUpInside];
	[btn1 setTitle:@"Same key search in Taobao/Kakaku" forState:UIControlStateNormal];
	UIButton* btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	btn2.frame = CGRectMake(50.0f, 120.0f, 220.0f, 40.0f);
	[btn2 addTarget:self action:@selector(pressBtn2) forControlEvents:UIControlEventTouchUpInside];
	[btn2 setTitle:@"Selected items into compare list" forState:UIControlStateNormal];
	UIButton* btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	btn3.frame = CGRectMake(50.0f, 190.0f, 220.0f, 40.0f);
	[btn3 addTarget:self action:@selector(presssBtn3) forControlEvents:UIControlEventTouchUpInside];
	[btn3 setTitle:@"Register keyword" forState:UIControlStateNormal];
	
	[self.view addSubview:btn1];
	[self.view addSubview:btn2];
	[self.view addSubview:btn3];*/
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

// --------------------------------------
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
	return 3;
}

- (UITableViewCell*) tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath {
	UITableViewCellStyle style = UITableViewCellStyleSubtitle;
	UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"BaseCell"];
	if (!cell) cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:@"BaseCell"] autorelease];
	
	switch (indexPath.row) {
		case 0:
			if (inTaobao) {NSLog(@"...he");
				cell.textLabel.text = @"Same key search in Japan";
			} else {
				cell.textLabel.text = @"Same key search in China";
			}
			
			break;
		case 1:
			cell.textLabel.text = @"Selected items into list";
			break;
		default:
			cell.textLabel.text = @"Register keyword";
			break;
	}
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//	NSLog(@"%d", indexPath.row);
	switch (indexPath.row) {
		case 0: {
			RootViewController* rvc = (RootViewController*) [[UIApplication sharedApplication] delegate];
			UINavigationController* navi = [rvc->search navigationController];
			[navi popToRootViewControllerAnimated:NO];
			
//			NSLog(@"key:%@", key);
			
			LKGoogleTranslator* translator = [[[LKGoogleTranslator alloc] init] autorelease];
			NSString* regi;
			if (trans) {
				if (inTaobao) {
					regi = [Register getKeyFromChina: key];
					if (regi != nil && ![regi isEqualToString:@"(null)"]) {
						key = [NSString stringWithString:regi];
					} else {
						//NSLog(@"%@~~~%@", key ,[translator translateText: key fromLanguage: kLKLanguageChinese toLanguage: kLKLanguageJapanese]);
						key = [translator translateText: key fromLanguage: kLKLanguageChinese toLanguage: kLKLanguageJapanese];
					}
				} else {
					regi = [Register getKeyFromJapan: key];
					if (regi != nil && ![regi isEqualToString:@"(null)"]) {
						key = [NSString stringWithString:regi];
					} else {
						key = [translator translateText: key fromLanguage: kLKLanguageJapanese toLanguage: kLKLanguageChinese];
					}
				}
			}
	//					NSLog(@"key:%@", key);
			inTaobao = !inTaobao;			
			[Taobao showSearchResult: 1l];		
			Result* result = [[Result alloc]init];
			[navi pushViewController:result animated:YES];			
			break;
		}
		case 1: {
			RootViewController* rvc = (RootViewController*) [[UIApplication sharedApplication] delegate];
			UINavigationController* navi = [rvc->search navigationController];
			[navi popViewControllerAnimated:NO];
			UIAlertView* av = [[[UIAlertView alloc] initWithTitle:@"Compare List" message:[NSString stringWithFormat:@"%d items is in list, view compare list?", [listItems count]]
														 delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil] autorelease];
			for (ItemList* aItem in items) {
				if (aItem.selected && [listItems count] < 31) {
					BOOL repeat = NO;
					for (ItemList* oItem in listItems) {
						if ([aItem.imageURL isEqualToString:oItem.imageURL] && [aItem.nick isEqualToString:oItem.nick] && [aItem.title isEqualToString:oItem.title] && [aItem.price isEqualToString:oItem.price]) {
							repeat = YES;
							break;
						}
					}
					if (repeat) continue;
					[listItems addObject: [[ItemList alloc] initWithImageURL:aItem.imageURL andNick:aItem.nick andTitle:aItem.title andPrice:aItem.price]];
				}
			}
			av.message = [NSString stringWithFormat:@"%d items is in list, view compare list?", [listItems count]];
			[av show];
			
			break;
		}
		default: {
			RootViewController* rvc = (RootViewController*) [[UIApplication sharedApplication] delegate];
			UINavigationController* navi = [rvc->search navigationController];
			[navi popViewControllerAnimated:NO];
			[self goReg];
			break;
		}
	}
}

- (void) alertView: (UIAlertView *) alertView clickedButtonAtIndex:(NSInteger) buttonIndex {
	if (buttonIndex == 1) {
		RootViewController* rvc = (RootViewController*) [[UIApplication sharedApplication] delegate];
		UINavigationController* navi = [rvc->search navigationController];
		CompareList* compare = [[CompareList alloc]init];
		[navi pushViewController:compare animated:YES];
		
	}
}

// --------------------------------------
- (void) goReg {
	RootViewController* rvc = (RootViewController*) [[UIApplication sharedApplication] delegate];
	UINavigationController* navi = [rvc->search navigationController];
	Register* regi = [[Register alloc]init];
	[navi pushViewController:regi animated:YES];
}
@end
